<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="项目名称">
                <a-input v-model="queryParam.itemName" allow-clear placeholder="请输入项目名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="项目分数">
                <a-input-number :min="0" v-model="queryParam.fullScore" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :md="10" :sm="24">
              <a-form-item label="类型">
                <a-radio-group button-style="solid" v-model="queryParam.category" @change="onChange">
                  <a-radio-button>全部</a-radio-button>
                  <a-radio-button :value="0">项目打分项</a-radio-button>
                  <a-radio-button :value="1">答辩打分项</a-radio-button>
                </a-radio-group>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator">
          <a-button type="primary" icon="plus" @click="$refs.detail.open()">新增打分项</a-button>
          <a-button type="danger" :disabled="selectedRowKeys.length < 1" @click="batchDelete"
          >
            <a-icon type="delete"/>
            批量删除
          </a-button
          >
        </template>

        <span slot="category" slot-scope="text, record">
          <a-tag color="purple" v-if="record.category == 0">项目打分项</a-tag>
          <a-tag color="green" v-if="record.category == 1">答辩打分项</a-tag>
        </span>

        <span slot="status" slot-scope="text, record">
          <a-switch
            :loading="swtichLoading"
            checked-children="正常"
            un-checked-children="停用"
            :defaultChecked="text === 0"
            :disabled="record.status == 2"
            @click="() => editScoreItemStatus(text, record)"
          />
        </span>

        <span slot="action" slot-scope="text, record" v-if="record.status != 2">
          <a @click="$refs.detail.open(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <score-item-detail ref="detail" @ok="handleOk"/>
    </a-card>
  </div>
</template>
<script>
  import {STable, XDown} from '@/components'
  import {scoreItemPage, scoreItemDelete, scoreItemChangeStatus} from '@/api/modular/main/scoreItemManage'
  import ScoreItemDetail from './detail.vue'

  export default {
    components: {
      STable,
      ScoreItemDetail,
      XDown,
    },
    data() {
      return {
        // 查询参数
        queryParam: {
          category: 0,
          status: 0,
        },
        swtichLoading: false,
        // 表头
        columns: [
          {
            title: '项目名称',
            align: 'center',
            dataIndex: 'itemName',
          },
          {
            title: '项目分数',
            align: 'center',
            dataIndex: 'fullScore',
          },
          {
            title: '打分项类型',
            align: 'center',
            dataIndex: 'category',
            scopedSlots: {customRender: 'category'},
          }
        ],
        tstyle: {'padding-bottom': '0px', 'margin-bottom': '10px'},
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          return scoreItemPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: {
            show: true,
            clear: () => {
              this.selectedRowKeys = []
            },
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
          },
        },
      }
    },
    created() {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: {customRender: 'action'},
      })
    },
    methods: {
      onChange(e) {
        this.handleOk()
      },

      /**
       * 修改项目状态
       */
      editScoreItemStatus(code, record) {
        let status = code == 0 ? 1 : 0
        let action = code == 0 ? '停用' : '启用'
        this.swtichLoading = true
        scoreItemChangeStatus({id: record.id, status: status})
          .then((res) => {
            if (res.success) {
              this.$message.success(action + '操作成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error(action + '操作失败：' + res.message)
            }
          })
          .finally(() => {
            this.swtichLoading = false
            this.$refs.table.refresh()
          })
      },
      /**
       * 单个删除
       */
      singleDelete(record) {
        const param = [{id: record.id}]
        this.scoreItemDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete() {
        const paramIds = this.selectedRowKeys.map((d) => {
          return {id: d}
        })
        this.scoreItemDelete(paramIds)
      },
      scoreItemDelete(record) {
        scoreItemDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      handleOk() {
        this.$refs.table.refresh()
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
    },
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
